package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.ILiquormallLiquorMapper;
import org.linlinjava.litemall.db.domain.ILiquormallLiquor;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ILiquormallLiquorService {
    @Resource
    private ILiquormallLiquorMapper iLiquormallLiquorMapper;


    public int deleteById(Integer userId, Integer id) {
        ILiquormallLiquor iLiquormallLiquor = iLiquormallLiquorMapper.selectByPrimaryKey(id);
        if (iLiquormallLiquor.getUserId().equals(userId)) {
            //是当前用户发布的酒源信息，可以删
            iLiquormallLiquorMapper.logicalDeleteByPrimaryKey(id);
            return 0;
        }
        return 1;
    }

    public ILiquormallLiquor queryById(Integer id) {
        return iLiquormallLiquorMapper.selectByPrimaryKey(id);
    }

    public ILiquormallLiquor queryLastOne() {
        return iLiquormallLiquorMapper.selectLastOne();
    }

    public void update(ILiquormallLiquor iLiquormallLiquor) {
        iLiquormallLiquorMapper.update(iLiquormallLiquor);
    }

    public int add(ILiquormallLiquor iLiquormallLiquor) {
        iLiquormallLiquor.setAddTime(LocalDateTime.now());
        iLiquormallLiquor.setUpdateTime(LocalDateTime.now());
        //获取最后一条记录
        ILiquormallLiquor lastiLiquormallLiquor = iLiquormallLiquorMapper.selectLastOne();
        int lastCountNumberOf;
        if (lastiLiquormallLiquor != null) {
            lastCountNumberOf = lastiLiquormallLiquor.getCountNumberOf();
        } else {
            lastCountNumberOf = 0;
        }
        iLiquormallLiquor.setCountNumberOf(lastCountNumberOf + iLiquormallLiquor.getNumberOf());
        iLiquormallLiquorMapper.insert(iLiquormallLiquor);
        if (lastiLiquormallLiquor != null && iLiquormallLiquorMapper.selectLastOne().getId().equals(lastiLiquormallLiquor.getId() + 1)) {
            return lastiLiquormallLiquor.getId() + 1;
        }
        return 0;
    }

    public ILiquormallLiquor getCountNumberOfLiquor() {
        return iLiquormallLiquorMapper.selectLastOne();
    }

    public List<ILiquormallLiquor> queryList() {
        return iLiquormallLiquorMapper.queryAllLiquor();
    }
    public List<ILiquormallLiquor> queryList(Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        return iLiquormallLiquorMapper.queryAllLiquorPage(page, limit);
    }

    public List<ILiquormallLiquor> queryListLimitFive() {
        return iLiquormallLiquorMapper.queryAllLiquorLimitFive();
    }

    public List<ILiquormallLiquor> queryListMy(int userId) {
        return iLiquormallLiquorMapper.queryAllLiquorMy(userId);
    }

    public List<ILiquormallLiquor> queryListBySit(int page, int limit, ILiquormallLiquor iLiquormallLiquor) {
        if (iLiquormallLiquor.getAge() != null && StringUtils.isEmpty(iLiquormallLiquor.getPrice())) {
            if (iLiquormallLiquor.getAge().equals("asc")) {
                //根据酒龄升序 asc
                PageHelper.startPage(page, limit);
                return iLiquormallLiquorMapper.queryAllLiquorByAgeAsc(iLiquormallLiquor);
            } else if (iLiquormallLiquor.getAge().equals("desc")) {
                //根据酒龄降序 desc
                PageHelper.startPage(page, limit);
                return iLiquormallLiquorMapper.queryAllLiquorByAgeDesc(iLiquormallLiquor);
            }
        } else if (iLiquormallLiquor.getPrice() != null && StringUtils.isEmpty(iLiquormallLiquor.getAge())) {
            if (iLiquormallLiquor.getPrice().equals("asc")) {
                //根据酒价升序
                PageHelper.startPage(page, limit);
                return iLiquormallLiquorMapper.queryAllLiquorByPriceAsc(iLiquormallLiquor);
            } else if (iLiquormallLiquor.getPrice().equals("desc")) {
                //根据酒价降序
                PageHelper.startPage(page, limit);
                return iLiquormallLiquorMapper.queryAllLiquorByPriceDesc(iLiquormallLiquor);
            }
        }
        PageHelper.startPage(page, limit);
        return iLiquormallLiquorMapper.queryAllLiquorBySit(iLiquormallLiquor);
    }

    /**
     * 计算酒友圈数量
     *
     * @return
     */
    public long count() {
        return iLiquormallLiquorMapper.count();
    }

    /**
     * 根据标题查询酒友圈
     * @param title
     * @param page
     * @param limit
     * @return
     */
    public List<ILiquormallLiquor> queryListByTitle(String title, int page, int limit) {
        PageHelper.startPage(page, limit);
        return iLiquormallLiquorMapper.queryAllLiquorByTitle(title);
    }

    public int deleteLiquor(int id) {
        return iLiquormallLiquorMapper.logicalDeleteByPrimaryKey(id);
    }


}
